<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\basic\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 危化工艺管理
 * @package app\Craft
 * @author LJQ<245369209@qq.com>
 */
class Craft extends Api

{
	protected $wfType = 12;
	

    /**
   *  危化工艺列表
   * @param string  string equipeId 企业id,如果有只显示企业自己的信息.
   * @return Json
   */

	public function craftList($scope = null){
        $where = [] ;
   
        $where['sourceType'] = ['eq',3] ;

        if($scope){
      
        	$where['enterpriseId'] = ['in',$scope];
        }
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 危化工艺列表
			$info =collection(model('Craft')->with('enterprise,sensors,staff')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			// 危化工艺列表
			$info =collection(model('Craft')->with('enterprise,sensors,staff')->where($where)->select())->toArray();
		}
	   	 
	 	//总数
	   	 $count = model('Craft')->where($where)->count('id'); 
		
    		if ($count == 0) {
         			return json(['code'=>1,'msg'=>'没有找到危化工艺','count'=>$count,'data'=>$info]);
       		} 
       	
       		if ($count > 0){
       			return json(['code'=>1,'msg'=>'危化工艺信息','count'=>$count,'data'=>$info]);
       		}
            		return json(['code'=>0,'msg'=>'数据异常']);
   	 	 }
   	  
   	      //添加危化工艺
   	 public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }

		 $model = model('Basic/craft');
		 if(($data['sensorIds'])){
			 $sensor=$data['sensorIds'];
		 }
		 $result = $model->allowField(true)->save($data);
		 $id=$model->id;
          //配置传感器
		 if($sensor){
			 if(model('Sensor')->where('dangerSourceId',$id)->select()){
				 model('Sensor')->where('dangerSourceId',$id)->update(['dangerSourceId'=>null]);//清空原来配置
			 }
			 model('Sensor')->where('id','in',$sensor)->update(['dangerSourceId'=>$id]);
		 }
          setlog('craftadd','craft',session('user_auth.id'));
          if (false != $result) {
              return json(['code'=>1,'msg'=>'危化工艺添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'危化工艺添加失败,请检查']);
          }
      }

   	

   /**
   * 危化工艺修改
   * @param  string CraftId 危化工艺id,
   * @return Json
   */
   	public function edit($data,$craftId){
        
          if (empty($craftId)) {
              return json(['code'=>0,'msg'=>'危化工艺不存在,请检查']);
          }
          //配置传感器
		if(!empty($data['sensorIds'][0])){

			model('Sensor')->where('dangerSourceId',$data['id'])->update(['dangerSourceId'=>null]);//清空原来配置
			if(model('Sensor')->where('id','in',$data['sensorIds'])->update(['dangerSourceId'=>$data['id']])){
				unset($data['sensorIds']);
			}else{
				return json(['code'=>0,'msg'=>'配置传感器失败']);
			}
		}
		$model = model('Basic/Craft');
		if ( $model->allowField(true)->save($data,['id'=>$craftId])) {
			setlog('Craftupdate','Craft',session('user_auth.id'));
			return json(['code'=>1,'msg'=>'危化工艺信息修改成功']);
		}else{
			return json(['code'=>0,'msg'=>'危化工艺信息修改失败,请检查']);
		}
    }
    /**
   *  删除危化工艺
   * @param string CraftId 危化工艺id,
   * @param Json
   */
     
    public function del($craftId) {
      	$CraftId = input('CraftId',0,'intval');
        $model = model('Basic/Craft');
      	if (empty ( $craftId )||empty(($model->where(array('id'=>$craftId))->find()))) {
           	return  json(['code'=>0,'msg'=>'危化工艺id不存在,请检查']);
        }
        if($model->destroy(['id'=>$craftId]) !== FALSE){
             setlog('craftdel','craft',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'危化工艺删除成功']);
       }else{
         	 return $this->json(['code'=>0,'msg'=>'危化工艺删除失败,请检查']);
       }
    }

     
   /**
   *   查询危化工艺
   * @param keyword危化工艺名称
   * @return Json
   */
   	public function search($keyword=null,$enterprise=null){
            $where = [] ;
   
            $where['sourceType'] = ['eq',3] ;
		if($keyword){
			
			$where['name'] = ['like','%'.$keyword.'%'];
		}

            if ($enterprise) {
                   $where['enterpriseId'] = ['eq',$enterprise];
            }
   		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//危化工艺列表
			$info =collection(model('Craft')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//危化工艺列表
			$info =collection(model('Craft')->where($where)->select())->toArray();
		}
	 	//总数
	       $count = model('Craft')->where($where)->count('id'); 
		 
    	if ($count == 0) {
            return json(['code'=>1,'msg'=>'没有找到危化工艺','count'=>$count,'data'=>$info]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'危化工艺','count'=>$count,'data'=>$info]);
       	}
     }

	/**
	 * 获取危化工艺位置
	 * @param string $enterpriseId
	 * @return Json
	 */
	public function getCraftPosition($craftId){
		$position = model('hazard')->where('id',$craftId)->value('latitudeLongitude');
		if($position){
			return json(['code'=>1,'msg'=>'','data'=>$position]);
		}else{
			return json(['code'=>0,'msg'=>'没有对应位置']);
		}
	}
     
     /**
      * 申请审核
      * @param string $hazardId
      */
     public function audit($craftId = null){
     	$workflow = model('Workflow')->where('wfType',$this->wfType)->find();
     	if(empty($workflow)||empty($workflow->steps)){
     		return json(['code'=>0,'msg'=>'无对应审核流程','count'=>'','data'=>'']);
     	}else{
	     	$craft = model('Craft')->get($craftId);
	     	if($craft->isUpdate(true)->save(['checkStatus'=>2]) !== false){
	     		$data['title'] = $craft['name'];
	     		$data['wfFlowInfoId'] = $workflow['id'];
	     		$data['wfType'] = $this->wfType;
	     		$data['busiId'] = $craftId;
	     		$data['enterpriseId'] = $craft['enterpriseId'];
	     		$data['applyPerson'] = session('user_auth.id');
	     		if(model('Wfitem')->save($data)){
                       
	     			return json(['code'=>1,'msg'=>'申请成功','count'=>'','data'=>'']);
	     		}else{
	     			return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
	     		}
	     	}
     	}
     	return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
     	 
     }
}